from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
from yuewei_selenium import biz_mysql


def yuewei_selenium(Novel_name, insert_time, Chapter, Extension_name, Platform):
    driver = webdriver.Chrome()
    driver.get("http://novel.ywread.net/admin.php/agent/index/index.html")
    driver.maximize_window()
    driver.implicitly_wait(20)

    # 定位账号
    driver.find_element_by_xpath('//*[@id="login-username"]').clear()
    driver.find_element_by_xpath('//*[@id="login-username"]').send_keys(username)
    # 定位密码
    driver.find_element_by_xpath('//*[@id="login-password"]').clear()
    driver.find_element_by_xpath('//*[@id="login-password"]').send_keys(password)
    # 点击登录
    driver.find_element_by_xpath('//button').click()

    # 小说列表
    try:
        time.sleep(0.5)
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//span[text()="小说列表"]'))
        )
        driver.find_element_by_xpath('//span[text()="小说列表"]').click()
    except Exception:
        print("点击小说列表")

    # 输入小说名称
    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//input[@name="keyword"]'))
        )
        driver.find_element_by_xpath('//input[@name="keyword"]').clear()
        driver.find_element_by_xpath('//input[@name="keyword"]').send_keys(Novel_name)
        driver.find_element_by_xpath('//input[@name="keyword"]').send_keys(Keys.ENTER)
    except Exception:
        print("点击输入小说名称")

    # 点击小说名
    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//td[3]/a'))
        )
        driver.find_element_by_xpath('//td[3]/a').click()
    except Exception:
        print("点击小说小说名")

    # 新页面
    driver.switch_to.window(driver.window_handles[-1])

    # 生成推广链接
    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//tr[1]//a[@title="生成推广链接，当前章链接"]'))
        )
        driver.find_element_by_xpath('//tr[1]//a[@title="生成推广链接，当前章链接"]').click()
    except Exception:
        print("点击生成推广链接")

    driver.switch_to_frame(driver.find_element_by_xpath('//*[@id="layui-layer-iframe1"]'))

    # 派单渠道名称
    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="name"]'))
        )
        driver.find_element_by_xpath('//*[@id="name"]').clear()
        driver.find_element_by_xpath('//*[@id="name"]').send_keys(str(insert_time) + str(Extension_name))
    except Exception:
        print("点击派单渠道名称")

    # 关注章节
    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="gzzj"]'))
        )
        driver.find_element_by_xpath('//*[@id="gzzj"]').clear()
        driver.find_element_by_xpath('//*[@id="gzzj"]').send_keys(8)
    except Exception:
        print("点击关注章节")

    # 提交
    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//button[@class="btn btn-minw btn-primary ajax-post"]'))
        )
        driver.find_element_by_xpath('//button[@class="btn btn-minw btn-primary ajax-post"]').click()
    except Exception:
        print("点击提交")

    # 获取url
    try:
        time.sleep(2)
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//div[@class="text-primary link-url"]'))
        )
        url = driver.find_element_by_xpath('//div[@class="text-primary link-url"]').text
        print(url)
        try:
            biz_mysql.insert_novel_mysql(url=url, Public_platform=Platform, Novel_name=Novel_name, Chapter=Chapter,
                                         datetime=str(insert_time), Extension_name=Extension_name)

        except Exception as e:
            print(e)
            print("数据入库失败")
    except Exception:
        print("点击获取url")

    # 取消
    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="get-referral-link-qrcode-modal"]//span[text()="×"]'))
        )
        driver.find_element_by_xpath('//*[@id="get-referral-link-qrcode-modal"]//span[text()="×"]').click()
    except Exception:
        print("点击取消")

    # 退出当前iframe
    driver.switch_to.default_content()

    driver.quit()
